define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
        require("jquery_ui");
    var imageTemp = require("./template/imageTemp.html");
    var progressTemp=require("./template/progress.html");
    var filesTemp = require("./template/files.html");
    var render_item = template.compile(imageTemp);
    var render_progress = template.compile(progressTemp);
    var render_files = template.compile(filesTemp);
    var uploadimte=null; 

    if(window.files_data){
        if(window.files_data.file_item.length>0){
            $(".js_fileList",".js_upload_container").append(render_files(window.files_data));
        }
    }
    
    if(window.upload_config){
        $(".js_new_upload").each(function(){
            var $el=$(this),$form=$el.closest('.js_upload_container'),sname=$el.data("submitname"),
            upload_path=$el.data("uploadpath"),deletepath=$el.data("delpath"),count=$el.data("count"),datas=eval('(' + $el.data("data") + ')')||null,
            extensions = $el.data("extensions"),maxSize =  $el.data("maxsize");
            console.log(extensions);
            var max_count=count-($form.find("li.imgbox").length || 0);
            if(count==undefined || count==""){
                max_count=null;
            }
           $el.uploader({
                multi: true,
                url: upload_path,
                delete_path:deletepath,
                max_count:max_count,
                max_count_tips:count,
                dropElement:"drop",
                extensions:window.upload_config.extensions,
                max_file_size:window.upload_config.maxSize,
                data:datas,
                FilesAdded: function(up, files) {
                    $.each(files, function(index, file) {
                        //var m = method.getFormatDateStrNow();

                        $(".js_file_upload_queue",$form).append(render_progress({
                            id:file.id,
                            filename:file.name,
                            filesize:Math.ceil(file.size/1024)
                        }));
                        
                    });
                },
                UploadProgress: function(up, file) {
                    var upfileid = "upload{0}".format(file.id);
                    uploadimte = $("#{0}".format(upfileid));
                    if (uploadimte.length) {
                        var percent = file.percent + "%";
                        $(".data", uploadimte).text(percent)
                        $(".uploadify-progress-bar", uploadimte).width(percent)
                    };

                },
                FileUploaded: function(up, file, res) { 
                    if ($.isPlainObject(res)) {
                        var fileType = method.getfileExt(file.name).toString() || false;
                        if(fileType == "jpg" || fileType == "gif" || fileType == "png" || fileType == "jpeg" ){
                            res.file.explorer = true;
                        }else{
                            res.file.explorer = false;
                        }
                        
                         res.file.progressid=file.id;
                         res.file.sname=sname;
                         res.file.deletepath=deletepath;
                         res.file.fileType = fileType;
                         res.file.fileName = file.name;
                         res.file.index=$form.find("li.imgbox").length;
                        console.log(res.file);
                        $(".js_fileList",$form).append(render_item(res.file));
                        uploadimte.remove();
                    } else {
                        uploadimte.addClass('uploadify-error');
                        uploadimte.remove();
                        up.files.removeFile(file);
                        config.msg.info(res || config.lang.uplodError);
                    } 
                },
                UploadComplete: function(up, files) {
                   $(".uploadify-progress-bar", uploadimte).remove();
                }
            }, "other");
            
            $("ul.ipost-list",$form).sortable({
                opacity: .8
            });

            if('draggable' in document.createElement('chooseDocument')){
                if(typeof window.FileReader =='undefinde'){
                    return;
                }
                var holder = document.getElementById('chooseDocument');
                var drop = document.getElementById('drop');
                holder.ondragover = function () {
                    $("#drop").removeClass("hide");
                    return false; 
                };
                drop.ondragenter  = function () { 
                    $("#drop .dorp-dis").html("<strong>释放鼠标</strong>");
                    return false; 
                };
                holder.ondragleave  = function(){
                    $("#drop .dorp-dis").html("<strong>请将文件拖拽至此区域</strong>");
                    return false;
                };

                window.ondrop = function (event) {
                    event.preventDefault();
                    $("#drop").addClass("hide");
                    $("#drop .dorp-dis").html("<strong>请将文件拖拽至此区域</strong>");
                    var files = event.dataTransfer.files;
                    return false;
                };
            }

            $(document).on("click", "a.item_new_close", function(e) {
                var t = $(this),progressid=t.data("progressid"),delpath=t.data("delpath");
                $.post(delpath, {
                    id: t.data("post-id"),
                    url: t.data("path")
                });
                var $box = $(e.target).closest("li.imgbox");
                $box.fadeOut(function() {
                    $box.remove();
                    $("#upload"+progressid).remove();

                    var maxcount=count-($form.find("li.imgbox").length || 0),files=0;
                    $form.find("li.imgbox").each(function(){
                        if($(this).find("a.item_new_close").data("progressid")){
                            files++;
                        }
                    });
                    if(count){
                        if(progressid){
                            $.data($el, 'uploader').plupload.removeFile(progressid)
                        }else{
                            $el.uploader("update",{max_count:maxcount+files});
                        }
                    }
                });
            });
        });
    }
});